package base.thread.jdkapidemo;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class AtomicCounterTest extends Thread {

	AtomicCounter counter;
	CyclicBarrier barrier;
	public AtomicCounterTest(AtomicCounter counter,CyclicBarrier barrier) {
		this.counter = counter;
		this.barrier=barrier;
	}
//	static Map<Integer, String> map=new HashMap<Integer, String>();

	public void run() {
		for(int i=0;i<110;i++){
			int t = counter.increment();
//			synchronized(map){
//				map.put(t, "");
//			}
		}
		try {
			barrier.await();
		} catch (Exception e) {
			e.printStackTrace();
		} 
		
	}

	public static void main(String[] args) throws Exception{
		AtomicCounter counter = new AtomicCounter();
		int len=100;
		CyclicBarrier barrier=new CyclicBarrier(len+1);
		for (int i = 0; i < len; i++) {
			new AtomicCounterTest(counter,barrier).start();
		}
		barrier.await();
		System.out.println(counter.getValue());
//		System.out.println(map.size());
	}

}
